package com.example.myapplication;


import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.utils.LogUtils;

public class SecondActivity extends AppCompatActivity {
    private EditText mEdit;
    private ImageButton mImageButton;
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private RadioButton mRadioButton4;

    private CheckBox mCheckBox1;
    private CheckBox mCheckBox2;
    private CheckBox mCheckBox3;
    private Switch mSwitch;
    private ProgressBar mProgressBar;
    private Button mProgressButton;
    private int progress;
    private int secondProgress;
    private int max;
    private SeekBar mSeekBar;
    private TextView mSeekText;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d("test1", "-------------SecondActivity Create---------------");
        setContentView(R.layout.activity_second);
        mEdit = findViewById(R.id.edittext);
        mImageButton = findViewById(R.id.imagebutton);
        mImageButton.setVisibility(View.GONE);
        mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEdit.setText("");
            }
        });
        mEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImageButton.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE);
            }
        });
        mRadioGroup = findViewById(R.id.radiogroup);
        mRadioButton1 = findViewById(R.id.rb1);
        mRadioButton2 = findViewById(R.id.rb2);
        mRadioButton3 = findViewById(R.id.rb3);
        mRadioButton4 = findViewById(R.id.rb4);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(mRadioButton1.isChecked()) {
                    mRadioButton1.setButtonDrawable(getDrawable(R.mipmap.bg_delete));
                    mRadioButton1.setTextColor(Color.YELLOW);
                } else {
                    mRadioButton1.setButtonDrawable(getDrawable(R.mipmap.bg_delete_pressed));
                    mRadioButton1.setTextColor(Color.parseColor("#ff0000"));
                }
                if (mRadioButton4.isChecked()) {
                    mRadioButton4.setBackgroundResource(R.mipmap.bg_yellow);
                } else {
                    mRadioButton4.setBackground(getDrawable(R.mipmap.bg_blue));
                }
            }
        });
        mCheckBox1 = findViewById(R.id.checkbox1);
        mCheckBox2 = findViewById(R.id.checkbox2);
        mCheckBox3 = findViewById(R.id.checkbox3);
        mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mCheckBox1.setButtonDrawable(R.mipmap.bg_delete);
                } else {
                    mCheckBox1.setButtonDrawable(R.mipmap.bg_delete_pressed);
                }
            }
        });
        mCheckBox2.setBackgroundResource(R.drawable.radio_bg_selector);

        mSwitch = findViewById(R.id.switch1);
        mSwitch.setShowText(true);
        mSwitch.setTextOn("ON");
        mSwitch.setTextOff("OFF");
        mSwitch.setTrackDrawable(getDrawable(R.mipmap.bg_yellow));
        mSwitch.setThumbDrawable(getDrawable(R.mipmap.bg_delete_pressed));
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtils.d("test11", "当前开关的状态是:" + isChecked);
            }
        });
        mProgressBar = findViewById(R.id.progress);
        progress = mProgressBar.getProgress();
        max = mProgressBar.getMax();
        secondProgress = mProgressBar.getSecondaryProgress();
        mProgressButton = findViewById(R.id.progress_button);
        mProgressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //mProgressBar.setProgress(++progress);
                //mProgressBar.setSecondaryProgress(++secondProgress);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while(progress < max) {
                            mProgressBar.setProgress(++progress);
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }).start();
            }
        });

        mSeekBar = findViewById(R.id.seekbar);
        mSeekText = findViewById(R.id.seek_text);
        mSeekBar.setMax(120);
        mSeekBar.setThumb(getDrawable(R.mipmap.bg_delete_pressed));
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mSeekText.setText("当前的进度为:" + mSeekBar.getProgress() + "/" + mSeekBar.getMax());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                LogUtils.d("test11", "==========开始拖动======");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(SecondActivity.this, "结束拖动，进度:" + mSeekBar.getProgress() + "/" + mSeekBar.getMax(), Toast.LENGTH_LONG).show();
            }
        });
    }
}